/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ReportFrame.java
 *
 * Created on Feb 11, 2011, 4:37:05 PM
 */

package gbbmgmt;

import java.sql.*;
import java.util.*;
import java.text.*;

/**
 *
 * @author dchapman
 */
public class ReportFrame extends javax.swing.JFrame {

    NumberFormat f2 = new DecimalFormat("#0.00");
    NumberFormat f1 = new DecimalFormat("#0");

    /** Creates new form ReportFrame */
    public ReportFrame() {
        initComponents();
    }

    public class order implements Comparable{
        public int VendID;
        public int WHID;
        public int TblID;
        public int Quant;

        public order(int VendID, int WHID, int TblID, int Quant){
            this.VendID = VendID;
            this.WHID = WHID;
            this.TblID = TblID;
            this.Quant = Quant;
        }

        //Overload compareTo Method.
        public int compareTo(Object obj){
            order tmp = (order)obj;
            if(this.VendID < tmp.VendID){
                return -1;
            } else if (this.VendID > tmp.VendID){
                return 1;
            }
            return 0;
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jTextArea1 = new javax.swing.JTextArea();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setName("Form"); // NOI18N

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        jTextArea1.setColumns(20);
        jTextArea1.setRows(5);
        jTextArea1.setName("jTextArea1"); // NOI18N
        jScrollPane1.setViewportView(jTextArea1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 618, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 472, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new ReportFrame().setVisible(true);
            }
        });
    }

    public void DisplayStrArr(ArrayList<String[]> DispArr){

        String TxtToDisp = "Over Due Sales Order Report\n";

        for(String s[] : DispArr){
            TxtToDisp += "Customer: " + s[0] + " Due Date: " + s[1] +
                    " Sales Order ID# " + s[2] + "\n";
        }

        jTextArea1.setText(TxtToDisp);
    }

    public void DisplayPOs(String POCSV){

    }

    public void DisplayTrialKit(String MCSV, String HCSV , String POCSV){

        String[] sarry = POCSV.split(",");
        int count = 0;
        String TxtToDisp = "Trial Kit Shortages For\n";

        //Open Database
        DBA.openDBCon();

        while (count < sarry.length){
            //Get Recipe information from the Beer ID.
            DBA.GetBeerData(sarry[count]);
            DBA.GetRecipesData(Integer.toString(DBA.RecipeID));
            TxtToDisp += sarry[count+1] +
                    "x Brews of " + DBA.BName + "\n";            
            count += 2;
        }

        TxtToDisp += "\n--- Malt Shortages ---\n";
        if (MCSV.length()>0){
            String[] marry = MCSV.split(",");
            count = 0;
            while (count < marry.length){
                DBA.GetMaltData(marry[count]);
                String SC = DBA.ReturnStockCode("100", marry[count]);
                String StockNumber = String.format("%05d", Integer.parseInt(SC));
                TxtToDisp += StockNumber + ": " + DBA.MaltName + " - " + marry[count+1] + " Bags\n";
                count += 2;
            }
        } else {
            TxtToDisp += "No Malt Shortages\n";
        }

        TxtToDisp += "\n--- Hop Shortages ---\n";
        if (HCSV.length()>0){
            String[] harry = HCSV.split(",");
            count = 0;
            while (count < harry.length){
                DBA.GetHopsData(harry[count]);
                String SC = DBA.ReturnStockCode("110", harry[count]);
                String StockNumber = String.format("%05d", Integer.parseInt(SC));
                TxtToDisp += StockNumber + ": " + DBA.HopName + " - " + harry[count+1] + " Bags\n";
                count += 2;
            }
        } else {
            TxtToDisp += "No Hop Shortages\n";
        }

        //Close Database
        DBA.closeDBCon();

        jTextArea1.setText(TxtToDisp);
    }

    public static String leftPad(String s, int width){
        return String.format("%-" + width + "s", s);
    }

    public void DisplayPOOrderList(String CSV){
        //Put the POCSV into an array.
        String[] poarry = CSV.split(",");
        String TxtToDisp = "Purchase Order List\n";

        order[] OrderLines = new order[poarry.length];

        DBA.openDBCon();

        int count = 0;
        while(count < poarry.length){
            //Look through PO Lines.
            DBA.GetPOLinesData(poarry[count]);
            DBA.GetStockCodeData(Integer.toString(DBA.POLPN));

            int vid = DBA.StockVendID;
            int wid = DBA.WHID;
            int tid = DBA.TableID;
            int qty = DBA.POLQty;

            OrderLines[count] = new order(vid,wid,tid,qty);

            count++;
        }

        //Sort array.
        Arrays.sort(OrderLines);

        //Print out lines based on vendors.
        for(int i=0; i<OrderLines.length; i++){
            String Desc = "";
            if (OrderLines[i].WHID == 100){
                DBA.GetMaltData(Integer.toString(OrderLines[i].TblID));
                Desc = DBA.MaltName;
            } else if (OrderLines[i].WHID == 110){
                DBA.GetHopsData(Integer.toString(OrderLines[i].TblID));
                Desc = DBA.HopName;
            }  else if (OrderLines[i].WHID == 120){
                DBA.GetYeastData(Integer.toString(OrderLines[i].TblID));
                Desc = DBA.YeastDesc;
            } else if (OrderLines[i].WHID > 130){
                DBA.GetSuppliesData(Integer.toString(OrderLines[i].TblID));
                Desc = DBA.SupName + " - " + DBA.SupNumber;
            }
            DBA.GetVendorData(Integer.toString(OrderLines[i].VendID));
            TxtToDisp += DBA.VName + " - " +Integer.toString(OrderLines[i].Quant) +
                    " - " + Desc + "\n";
        }

        DBA.closeDBCon();

        jTextArea1.setText(TxtToDisp);

    }

    public void DisplayPOArr(ArrayList<String[]> DispMalts, ArrayList<String[]> DispHops){
        String TxtToDisp = "-- Malt -- \n";
        for(String s[] : DispMalts){
            if(Double.valueOf(s[0].trim()).doubleValue() > 0){
                double lbs = Double.valueOf(s[0].trim()).doubleValue();
                double onHand = Double.valueOf(s[4].trim()).doubleValue();
                double spoken = Double.valueOf(s[5].trim()).doubleValue();
                double bag = Double.valueOf(s[6].trim()).doubleValue();
                double value = lbs - (onHand - spoken);
                if(value<0) value=0;
                if(value != 0){
                    double bags = Math.ceil(value/bag);
                    TxtToDisp += f2.format(value) + " Lbs. (";
                    TxtToDisp += f1.format(bags) + " Bags) of " + s[2];
                    TxtToDisp += "\n";
                }
            }
        }
        TxtToDisp += "\n -- Hops -- \n";
        for(String s[] : DispHops){
            if(Double.valueOf(s[0].trim()).doubleValue() > 0){
                double lbs = Double.valueOf(s[0].trim()).doubleValue();
                double onHand = Double.valueOf(s[4].trim()).doubleValue();
                double spoken = Double.valueOf(s[5].trim()).doubleValue();
                double bag = Double.valueOf(s[6].trim()).doubleValue();
                double value = lbs - (onHand - spoken);
                if(value<0) value=0;
                if(value != 0){
                    double bags = Math.ceil(value/bag);
                    TxtToDisp += f2.format(value) + " Lbs. (";
                    TxtToDisp += f1.format(bags) + " Bags) of " + s[2];
                    TxtToDisp += "\n";
                }
            }
        }
        jTextArea1.setText(TxtToDisp);
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextArea jTextArea1;
    // End of variables declaration//GEN-END:variables

}
